use std::sync::Arc;
use crate::student::Student;

const CHAIR_COUNT: usize = 3;
///长廊
pub(crate) struct Gallery {
    chairs: Vec<Chair>,
}
impl Gallery {
    pub fn new() -> Self {
        Self {
            chairs: vec![Chair::new(); CHAIR_COUNT],
        }
    }
    pub fn has_empty_chair(&self) -> bool {
        self.chairs.iter().any(|chair| !chair.is_used())
    }
    pub fn get_first_used_chair(&mut self) -> Option<Arc<Student>> {
        if self.chairs.first().is_some_and(|c| c.is_used()) {
            let first_chair = self.chairs.remove(0);
            self.chairs.push(Chair::new());
            if first_chair.is_used() {
                first_chair.student
            } else {
                None
            }
        } else {
            None
        }
    }
    pub fn site_down(&mut self, student: Arc<Student>) {
        println!("学生:{}坐在了椅子上", student.name());
        for s in self.chairs.iter_mut() {
            if !s.used {
                s.set_used(student);
                break;
            }
        }
    }
}

#[derive(Debug, Clone)]
struct Chair {
    used: bool,
    student: Option<Arc<Student>>,
}
impl Chair {
    pub fn new() -> Self {
        Self {
            used: false,
            student: None,
        }
    }
    pub fn is_used(&self) -> bool {
        self.used
    }
    pub fn set_used(&mut self, student: Arc<Student>) {
        self.used = true;
        self.student = Some(student);
    }
}
